package handel

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"teacher-perm/model"
)

func Add(c *gin.Context) {
	house_title := c.PostForm("house_title")
	house_content := c.PostForm("house_content")
	house_price, _ := strconv.Atoi(c.PostForm("house_price"))
	if house_title == "" || house_content == "" || house_price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "参数错误",
		})
		return
	}
	data := model.House{
		Title:   house_title,
		Content: house_content,
		Price:   house_price,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var house []model.House
	model.DB.Offset(offset).Limit(size).Find(&house)
	var total int64
	model.DB.Model(&model.House{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  house,
		"total": total,
	})
}

/*func Add(c *gin.Context) {
	article_title := c.PostForm("article_title")
	article_content := c.PostForm("article_content")
	article_author := c.PostForm("article_author")
	if article_title == "" || article_content == "" || article_author == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "参数不正确",
		})
		return
	}
	data := models.Article{
		Title:   article_title,
		Content: article_content,
		Author:  article_author,
	}
	err := models.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("page_size"))
	offset := (page - 1) * size

	var article []models.Article
	models.DB.Offset(offset).Limit(size).Find(&article)
	var total int64
	models.DB.Model(&models.Article{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  article,
		"total": total,
	})
	return
}*/
